package com.xinou.daily_report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.common.util.BeanUtil;
import com.xinou.daily_report.PO.NotificationPO;
import com.xinou.daily_report.PO.NotificationUpdatePO;
import com.xinou.daily_report.VO.NotificationVO;
import com.xinou.daily_report.entity.SysNotification;
import com.xinou.daily_report.mapper.SysNotificationMapper;
import com.xinou.daily_report.service.SysNotificationService;
import com.xinou.sso.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @创建人 wdl
 * @创建时间 2021/4/28
 * @描述
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysNotificationServiceImpl extends ServiceImpl<SysNotificationMapper, SysNotification> implements SysNotificationService {

    private final SysUserService sysUserService;


    @Override
    public ApiResponse<NotificationVO> notificationList(NotificationPO po) {
        //根据用户id，获得用户的真实姓名
        Map<Long, String> realNameByUserId = sysUserService.getRealNameByUserIds(sysUserService.userAll().stream().map(k -> k.getUserId()).collect(Collectors.toSet()));
        Page<SysNotification> page = new Page<>(po.getPageNumber(),po.getPageSize());
        Page<SysNotification> SysNotificationPage = page(
                page,
                new LambdaQueryWrapper<SysNotification>()
                        .isNotNull(SysNotification::getId)
                .eq(po.getUserId()!=null,SysNotification::getUserId,po.getUserId())
                .orderByDesc(SysNotification::getSort)
                .orderByDesc(SysNotification::getInsertTime)
        );
        List<NotificationVO> notificationVOS = BeanUtil.listA2ListB(SysNotificationPage.getRecords(), NotificationVO.class);
        for (NotificationVO notificationVO : notificationVOS) {
            notificationVO.setUserName(realNameByUserId.get(notificationVO.getUserId()));
        }

        return new ApiResponse<NotificationVO>(
                notificationVOS,
                SysNotificationPage.getTotal());
    }



    @Override
    public ApiResponse<Void> notificationDelete(NotificationPO po) {
        boolean flag = removeById(po.getId());
        if(!flag){
            throw new BusException(HttpStatusEnum.RE_CODE_DATABASE_ERROR,"删除通报失败");
        }
        return new ApiResponse<Void>();
    }

    @Override
    public ApiResponse<NotificationVO> notificationGetById(NotificationPO notificationPO) {
        return new ApiResponse<NotificationVO>(BeanUtil.beanA2beanB(getById(notificationPO.getId()),NotificationVO.class));
    }


    @Override
    public ApiResponse<Void> notificationSave(NotificationPO po) {
        if(po.getInsertTime()==null){
            throw new BusException(HttpStatusEnum.RE_CODE_DATABASE_ERROR,"添加通报失败,请选择时间");
        }
        if(po.getMatter().length()>300){
            throw new BusException(HttpStatusEnum.RE_CODE_DATABASE_ERROR,"添加通报失败,事项长度过长");
        }
        boolean flag = save(BeanUtil.beanA2beanB(po,SysNotification.class));
        if(!flag){
            throw new BusException(HttpStatusEnum.RE_CODE_DATABASE_ERROR,"添加通报失败");
        }
        return new ApiResponse<>();
    }


    @Override
    public ApiResponse<Void> notificationUpdate(NotificationUpdatePO po) {
        if(po.getInsertTime()==null){
            throw new BusException(HttpStatusEnum.RE_CODE_DATABASE_ERROR,"修改通报失败,请选择时间");
        }
        if(po.getMatter().length()>300){
            throw new BusException(HttpStatusEnum.RE_CODE_DATABASE_ERROR,"修改通报失败,事项长度过长");
        }
        boolean flag = updateById(BeanUtil.beanA2beanB(po,SysNotification.class));
        if(!flag){
            throw new BusException(HttpStatusEnum.RE_CODE_DATABASE_ERROR,"修改通报失败");
        }
        return new ApiResponse<>();
    }



    @Override
    public ApiResponse<NotificationVO> notificationInfo(NotificationPO po) {
        SysNotification byId = getById(po.getId());
        return new ApiResponse<NotificationVO>(BeanUtil.beanA2beanB(byId,NotificationVO.class));
    }

    @Override
    public ApiResponse<Void> notificationUpdateSingle(NotificationPO notificationPO) {
        SysNotification byId = getById(notificationPO.getId());
        byId.setSort(notificationPO.getSort());
        updateById(byId);
        return new ApiResponse<>();
    }

    @Override
    public ApiResponse<NotificationVO> notificationListDown(NotificationPO po) {
        //根据用户id，获得用户的真实姓名
        Map<Long, String> realNameByUserId = sysUserService.getRealNameByUserIds(sysUserService.userAll().stream().map(k -> k.getUserId()).collect(Collectors.toSet()));
        List<SysNotification> list = list(new LambdaQueryWrapper<SysNotification>()
                .isNotNull(SysNotification::getId)
                .eq(po.getUserId() != null, SysNotification::getUserId, po.getUserId())
                .orderByDesc(SysNotification::getSort));
        List<NotificationVO> notificationVOS = BeanUtil.listA2ListB(list, NotificationVO.class);
        for (NotificationVO notificationVO : notificationVOS) {
            notificationVO.setUserName(realNameByUserId.get(notificationVO.getUserId()));
        }

        return new ApiResponse<NotificationVO>(
                notificationVOS);

    }
}
